//
//  sfaAlarmListViewController.m
//  AlarmClock
//
//  Created by Fedor Skrynnikov on 9/10/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "sfaAlarmListViewController.h"
#import "EditAlarmViewController.h"
#import "sfaAppDelegate.h"
#import "Util.h"
#import "SQLiteUtil.h"

@interface sfaAlarmListViewController ()

@end

@implementation sfaAlarmListViewController

@synthesize alarmArray = _alarmArray;

#pragma mark -
#pragma mark life cicle methods


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        //_alarmArray = [[NSMutableArray alloc] initWithObjects:@"alarm1", @"alarm2", @"alarm3", @"alarm4", @"alarm5", nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setTitle:@"Alarm List"];
    UIBarButtonItem* addBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addButtonPressed:)];
    self.navigationItem.leftBarButtonItem = addBtn;
    [addBtn release];
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    SQLiteUtil* sqlite = [[SQLiteUtil alloc] init];
    if (self.alarmArray != nil) {
        [_alarmArray release];
    }
    self.alarmArray = [sqlite selectFromTable:@"alerts"];
    [sqlite release];
    [self.tableView reloadData];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_alarmArray count];
}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *myIdentifer = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifer];
    
    if(cell == nil){
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myIdentifer] autorelease];
    }
    if (cell != nil) {
        NSDictionary *record = [self.alarmArray objectAtIndex: indexPath.row];
        NSString* name = [[record objectForKey:@"name"] retain];
        [cell.textLabel setText: name];
        [name release];
        [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        SQLiteUtil* sqlite = [[SQLiteUtil alloc] init];
        NSDictionary* record = [_alarmArray objectAtIndex:indexPath.row];
        [sqlite deleteFromTable:@"alerts" record:[record objectForKey:@"recordId"]];
        [sqlite release];
        
        [_alarmArray removeObject:record];

        NSArray* deleteRows = [[NSArray alloc]initWithObjects:indexPath, nil];
        [self.tableView deleteRowsAtIndexPaths:deleteRows withRowAnimation:UITableViewRowAnimationRight];
        [deleteRows release];
    }
}

#pragma mark -
#pragma mark UITableViewDelegate

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    EditAlarmViewController *view = (EditAlarmViewController*)[Util allocViewWithClassName:@"EditAlarmViewController"];
    NSDictionary* record = [_alarmArray objectAtIndex: indexPath.row];
    NSString* title = [[record objectForKey:@"name"] retain];
    [view setTitle:title];
    view.initData = record;
    sfaAppDelegate *del = (sfaAppDelegate*)[UIApplication sharedApplication].delegate;
    [del.mainViewController pushViewController:view animated:YES];
    [title release];
    [view release];
}


- (IBAction)addButtonPressed:(id)sender{
    EditAlarmViewController *view = (EditAlarmViewController*)[Util allocViewWithClassName:@"EditAlarmViewController"];
    [view setTitle:@"New Alarm"];
    sfaAppDelegate *del = (sfaAppDelegate*)[UIApplication sharedApplication].delegate;
    [del.mainViewController pushViewController:view animated:YES];
    [view release];
}


@end
